package com.zretc.controller;


import com.zretc.entity.Department;

import com.zretc.entity.ResponseResult;
import com.zretc.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * 何亮龙
 * 部门控制层
 */
@RestController
@RequestMapping("/organization/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    @GetMapping("/getAllDepartments")
    public List<Department> getAllDepartments() {
        return departmentService.getAllDepartments();
    }
    @PostMapping("/addDep")
    public ResponseResult addDep(@RequestBody Department dep) {
        departmentService.addDep(dep);
        if (dep.getResult() == 1) {
            return ResponseResult.ok("添加成功", dep);
        }
        return ResponseResult.error("添加失败");
    }

    @DeleteMapping("/deleteDepById/{id}")
    public ResponseResult deleteDepById(@PathVariable Integer id) {
        Department dep = new Department();
        dep.setId(id);
        departmentService.deleteDepById(dep);
        if (dep.getResult() == -2) {
            return ResponseResult.error("该部门下有子部门，删除失败");
        } else if (dep.getResult() == -1) {
            return ResponseResult.error("该部门下有员工，删除失败");
        } else if (dep.getResult() == 1) {
            return ResponseResult.ok("删除成功");
        }
        return ResponseResult.error("删除失败");
    }
}